package com.gxk.actor;

import java.util.Random;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.gxk.res.ImageFactory;
import com.gxk.res.PublicConst;

public class BarActor extends Actor {
	private static final AtlasRegion REGION_BAR_UP;
	private static final AtlasRegion REGION_BAR_DOWN;

	static {
		REGION_BAR_UP = ImageFactory.getBarUpImage();
		REGION_BAR_DOWN = ImageFactory.getBarDownImage();
	}

	private float x;
	private float y;

	private int bar_H;

	private boolean isPass;
	private boolean isMove;

	private Rectangle barRectangle_Up;
	private Rectangle barRectangle_Down;

	public BarActor(float offx, float offy, boolean isMove) {

		this.x = PublicConst.WORLD_WIDTH + offx;
		this.y = 102f + offy;

		this.bar_H = new Random().nextInt(PublicConst.BAR_FLX_H)
				+ PublicConst.BAR_MIN_H;

		this.isMove = isMove;

		this.rectangleInit();
	}

	private void rectangleInit() {
		this.isPass = false;
		this.barRectangle_Down = new Rectangle(this.x + 1, this.y, 49, bar_H);
		this.barRectangle_Up = new Rectangle(this.x + 1, this.y + bar_H
				+ PublicConst.BAR_GAP_H, 49, 320);
	}

	public void setMoveState(boolean isMove) {
		this.isMove = isMove;
	}

	@Override
	public void act(float delta) {
		this.update();
		super.act(delta);
	}

	private void update() {

		this.rectangleUpdate();

		if (this.isMove == true) {
			this.x -= PublicConst.MOVE_SPEED;
			if (this.x < -1 * REGION_BAR_UP.getRegionWidth()) {
				this.isPass = false;
				this.bar_H = new Random().nextInt(PublicConst.BAR_FLX_H)
						+ PublicConst.BAR_MIN_H;
				this.x = PublicConst.WORLD_WIDTH;
			}
		}

	}

	private void rectangleUpdate() {
		this.barRectangle_Down.x = this.x + 1;
		this.barRectangle_Down.height = this.bar_H;

		this.barRectangle_Up.x = this.x + 1;
		this.barRectangle_Up.y = this.y + this.bar_H + PublicConst.BAR_GAP_H;
	}

	@Override
	public void draw(Batch batch, float parentAlpha) {
		batch.draw(new TextureRegion(REGION_BAR_DOWN, 0, 0, 52, bar_H), this.x,
				this.y);
		batch.draw(REGION_BAR_UP, this.x, this.y + bar_H
				+ PublicConst.BAR_GAP_H);
		super.draw(batch, parentAlpha);
	}

	public Rectangle getBarRectangle_Up() {
		return barRectangle_Up;
	}

	public Rectangle getBarRectangle_Down() {
		return barRectangle_Down;
	}

	public boolean isPass() {
		return isPass;
	}

	public void setPass(boolean isPass) {
		this.isPass = isPass;
	}

	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}
}
